#include <bits/stdc++.h>
// 2025/07/22
// tag:
// Author: Forever
using namespace std;
using int64 = int64_t;

int t, n, ans, poker[15];
unordered_map<unsigned long long, int> mp;

bool HASH(int dep){
    unsigned long long h = 0;
    for (int i = 0, p = 1; i < 15; i++, p *= 13331)
        h += poker[i] * p;
    if (mp.count(h) && mp[h] <= dep)
        return true;
    mp[h] = dep;
    return false;
}

void dfs(int dep)
{
    if (dep >= ans || HASH(dep))
        return;
    int res = dep;
    for (int i = 0; i < 15; ++i)
        if (poker[i])
            res++;
    if (poker[13] && poker[14])
        res--;
    ans = min(ans, res);
    int len1 = 0, len2 = 0, len3 = 0;
    for (int i = 0; i < 12; ++i) {
        len1 = (poker[i] >= 1 ? len1 + 1 : 0);
        len2 = (poker[i] >= 2 ? len2 + 1 : 0);
        len3 = (poker[i] >= 3 ? len3 + 1 : 0);
        if (len1 >= 5) {
            for (int j = 0; j < len1; ++j)
                poker[i - j] -= 1;
            dfs(dep + 1);
            for (int j = 0; j < len1; ++j)
                poker[i - j] += 1;
        }
        if (len2 >= 3) {
            for (int j = 0; j < len2; ++j)
                poker[i - j] -= 2;
            dfs(dep + 1);
            for (int j = 0; j < len2; ++j)
                poker[i - j] += 2;
        }
        if (len3 >= 2) {
            for (int j = 0; j < len3; ++j)
                poker[i - j] -= 3;
            dfs(dep + 1);
            for (int j = 0; j < len3; ++j)
                poker[i - j] += 3;
        }
    }

    for (int i = 0; i < 15; ++i) {
        if (poker[i] >= 3) {
            poker[i] -= 3;
            for (int j = 0; j < 15; ++j)
                if (poker[j] >= 2)
                    poker[j] -= 2, dfs(dep + 1), poker[j] += 2;
            for (int j = 0; j < 15; ++j)
                if (poker[j] >= 1)
                    poker[j]--, dfs(dep + 1), poker[j]++;
            dfs(dep + 1);
            poker[i] += 3;
        }
    }

    for (int i = 0; i <= 12; ++i) {
        if (poker[i] == 4) {
            poker[i] -= 4;
            for (int j = 0; j < 15; ++j) {
                if (poker[j] >= 2) {
                    poker[j] -= 2;
                    for (int k = j; k < 15; ++k)
                        if (poker[k] >= 2)
                            poker[k] -= 2, dfs(dep + 1), poker[k] += 2;
                    poker[j] += 2;
                }
                if (poker[j] >= 1) {
                    poker[j]--;
                    for (int k = j; k < 15; ++k)
                        if (poker[k] >= 1)
                            poker[k]--, dfs(dep + 1), poker[k]++;
                    poker[j]++;
                }
            }
            dfs(dep + 1);
            poker[i] += 4;
        }
    }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> t >> n;
    while (t--) {
        memset(poker, 0, sizeof(poker));
        mp.clear();
        ans = n;
        for (int i = 1; i <= n; i++) {
            int card, suit;
            cin >> card >> suit;
            if (3 <= card && card <= 13)
                poker[card - 3]++;
            else if (card == 1 || card == 2)
                poker[10 + card]++;
            else
                poker[12 + suit]++;
        }
        dfs(0);
        cout << ans << endl;
    }
    return 0;
}